package com.hsq.ndmall.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsq.ndmall.common.utils.PageUtils;
import com.hsq.ndmall.common.utils.Query;

import com.hsq.ndmall.order.dao.CartDao;
import com.hsq.ndmall.order.entity.CartEntity;
import com.hsq.ndmall.order.service.CartService;


@Service("cartService")
public class CartServiceImpl extends ServiceImpl<CartDao, CartEntity> implements CartService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CartEntity> page = this.page(
                new Query<CartEntity>().getPage(params),
                getQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    @Cacheable(value = "ndmall-order",key = "'query:cart:'+#userId")
    public List<CartEntity> queryByUidAndChecked(Integer userId) {
        return this.list(Wrappers.<CartEntity>lambdaQuery().eq(CartEntity::getChecked,true).eq(CartEntity::getDeleted,false));
    }


    @CacheEvict(value = "ndmall-order",key = "'query:cart:'+#userId")
    @Override
    public void clearGoods(Integer userId) {
        this.remove(Wrappers.<CartEntity>lambdaQuery().eq(CartEntity::getUserId,userId));
    }
    private QueryWrapper generatorWrapper(Map<String, Object> params){
        QueryWrapper<CartEntity> goodsEntityQueryWrapper = new QueryWrapper<>();
        if(params.containsKey("goods_id")){
            goodsEntityQueryWrapper.lambda().eq(CartEntity::getGoodsId,params.get("goods_id"));
        }
        if(params.containsKey("product_id")){
            goodsEntityQueryWrapper.lambda().eq(CartEntity::getProductId,params.get("product_id"));
        }
        if(params.containsKey("price")){
            goodsEntityQueryWrapper.lambda().eq(CartEntity::getPrice,params.get("price"));
        }
        if(params.containsKey("user_id")){
            goodsEntityQueryWrapper.lambda().eq(CartEntity::getUserId,params.get("user_id"));
        }
        //查询的商品必须是逻辑上未删除的商品
        goodsEntityQueryWrapper.lambda().eq(CartEntity::getDeleted,0);
        return goodsEntityQueryWrapper;

    }

    private QueryWrapper getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<>();
        if (params.containsKey("goods_id")) {
            queryWrapper.lambda().eq(CartEntity::getGoodsId, params.get("goods_id"));
        }
        if (params.containsKey("product_id")) {
            queryWrapper.lambda().eq(CartEntity::getProductId, params.get("product_id"));
        }
        if (params.containsKey("price")) {
            queryWrapper.lambda().eq(CartEntity::getPrice, params.get("price"));
        }
        if (params.containsKey("user_id")) {
            queryWrapper.lambda().eq(CartEntity::getUserId, params.get("user_id"));
        }
        if (params.containsKey("id")) {
            queryWrapper.lambda().eq(CartEntity::getId, params.get("id"));
        }
        return queryWrapper;
    }
}
